<?php
namespace app\mxty\controller;

use think\facade\Request;

use app\api\controller\Controller as ApiController;

use app\mxty\logic\Course as CourseLogic;

class Course extends ApiController
{
    // 获取所有课程
    // TODO: 剩多少报名名额，初始销量，用户端展示的销量 = 初始销量 + 实际销量
    public function list()
    {
        $list = (new CourseLogic)->getAllCourse();
        return $this->renderSuccess($list);
    }

    // 获取首页推荐课程
    public function recommends()
    {
        $list =  (new CourseLogic)->getRecommends();
        return $this->renderSuccess($list);
    }

    // 购买课程
    public function buy($courseId)
    {
        $res = (new CourseLogic)->buyCourse($courseId);
        return $this->renderSuccess($res);
    }

    // 获取指定课程的详情
    public function detail($courseId)
    {
        $course =  (new CourseLogic)->getCourseDetail($courseId);
        return $this->renderSuccess($course);
    }

    // 获取指定用户的所有课程
    public function userCourse()
    {
        $list = (new CourseLogic)->getUserCourses();
        return $this->renderSuccess($list);
    }

    // 获取指定教练的所有课程
    public function coachCourse()
    {
        $list = (new CourseLogic)->getCoachCourses();
        return $this->renderSuccess($list);
    }

    // 获取课程二维码token
    public function getCodeToken()
    {
        $codeToken = (new CourseLogic)->generateCodeToken();
        return $this->renderSuccess(compact('codeToken'));
    }

    // 课程二维码信息查询
    public function checkCourse($orderId, $token)
    {
        $course = (new CourseLogic)->checkCourse($orderId, $token);
        return $this->renderSuccess($course->toArray());
    }

    // 课程二维码签到
    public function clockIn($orderId, $token)
    {
        $isCreated = (new CourseLogic)->clockIn($orderId, $token);
        return $this->renderSuccess(compact('isCreated'));
    }

    // 获取指定课程的所有课时及其学员信息
    public function lessons($courseId)
    {
        $list = (new CourseLogic)->getAllLessonsWithStudents($courseId);
        return $this->renderSuccess($list);
    }
}
